package games.ninJump.ui.actions
{
	import actions.PropsAction;
	import actions.PropsActionGroup;
	
	import core.Global;
	
	import events.Msg_GameOver;
	
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import games.ninJump.NinJump;
	import games.ninJump.data.Animations;
	import games.ninJump.data.GameState;
	import games.ninJump.data.NinjaState;

	public class Ninja extends ActionTrans
	{
		//climbing.png,falling.png
		private static var INIT_Y:Number = 350;
		private static var LEFT_SIDE:Number = GameState.LEFT_SIDE - 10;
		private static var RIGHT_SIDE:Number = GameState.RIGHT_SIDE + 10;
		private static var SPEEDX:Number = .2;
		private static var TIME_LAST:Number;
		private static var GRAVITY:Number = .008;
		private static var SPEEDY:Number ;
		private var m_info:NinjaState;
		private var m_state:String;
		private var m_direction:String ;
		private var m_now:Number;
		private var m_over:Boolean;
		
		public function Ninja(info:NinjaState)
		{
			super();
			TIME_LAST = (RIGHT_SIDE - LEFT_SIDE) / SPEEDX
			m_info = info;
			m_info.addEventListener(Event.CHANGE,onChangeState)
			
		
		}
		
		public function init():void
		{
			x =  LEFT_SIDE;
			y = INIT_Y;
			state = m_info.state;
			m_over = false;
			
		}
		
		public function set state(name:String):void
		{
			m_state = name;
			var atom:* = Global.loadResource(NinJump.GAME_URL,name);
			var bmd:BitmapData = atom.obj;
			this.des = Animations.getAnimationByName(name);
			this.bmd = bmd;

			m_direction = x <= LEFT_SIDE ?"left":"right";
			if(m_direction == "left")
			{
				scaleX = 1;
			}
			else
			{
				scaleX = -1;				
			}
			m_now = getTimer(); 
			SPEEDY  = TIME_LAST * GRAVITY * .5;
		}
		
		override public function update(t:Object = null):void
		{
			super.update(t);
			if(m_state == NinjaState.NINJA_JUMPING )
				move(m_direction);
			if(m_state == NinjaState.NINJA_CLIMBING)
			{
				checkHitEnemy();
			}
			
			if(m_state == NinjaState.NINJA_FALLING && !m_over)
			{
				trace("****************888");
				m_over = true;
				/*var pa:PropsAction = new PropsAction(target);
				pa.props = [{prop:"x",init:x,end:GameState.GAME_WIDTH / 3},{prop:"y",init:y,end:GameState.GAME_HEIGHT - 400}];
				pa.speed = .01;
				pa.acceleration = 0;
				
				var pa2:PropsAction = new PropsAction(target);
				//pa2.props = [ {prop:"alpha",init:1,end:1}];
				pa2.speed = .01;
				pa2.acceleration = 0;
				
				var pa1:PropsAction = new PropsAction(target);
				pa1.props = [ {prop:"y",init:GameState.GAME_HEIGHT - 400,end:GameState.GAME_HEIGHT}];
				pa1.speed = .01;
				pa1.acceleration = 0;
				
				
				var pag:PropsActionGroup = new PropsActionGroup();
				var v:Vector.<PropsAction> = new Vector.<PropsAction>();
				v.push(pa,pa2,pa1);
				pag.list = v;
				pag.type = PropsActionGroup.SERIAL;
				
				Global.addAction(pag);
				*/
			}
	
		}
		
		public function get state():String
		{
			return m_state;
		}
		
		private function move(dir:String):void
		{
			var t:Number = getTimer() - m_now;
			if(dir == "left")
			{
				x += SPEEDX * t;
				y -= SPEEDY ;
				SPEEDY -= GRAVITY *  t;
				if(x > RIGHT_SIDE )
				{
					x = RIGHT_SIDE ;
					y = INIT_Y ;
					m_info.state = NinjaState.NINJA_CLIMBING;
				}
			}else
			{
				x -= SPEEDX * t;				
				y -= SPEEDY ;
				SPEEDY -= GRAVITY *  t;
				if(x < LEFT_SIDE)
				{
					x = LEFT_SIDE ;
					y = INIT_Y ;
					m_info.state = NinjaState.NINJA_CLIMBING;
				}
			}
			
		}
		
		private function onChangeState(e:Event):void
		{
			state = e.target.state;
			
		}
		
		private function checkHitEnemy():void
		{
			
			if(!bmdCopy  )
			{
				return;
			}
			for(var i:int = 0; i < GameState.ENEMIES.length; i++)
			{
				if(!GameState.ENEMIES[i].bmdCopy )continue;
				var ninPoint:Point;
				var testPoint:Point;
				if(GameState.ENEMIES[i].scaleX > 0)
				{
					
					testPoint = new Point(GameState.ENEMIES[i].x ,GameState.ENEMIES[i].y)
				}else
				{
					testPoint = new Point(GameState.ENEMIES[i].x - GameState.ENEMIES[i].width ,GameState.ENEMIES[i].y)
				}
				
				if(scaleX > 0)
				{
					ninPoint = new Point(x,y);
				}else
				{
					ninPoint = new Point(x - width,y)
				}
				if(bmdCopy.hitTest(ninPoint,0xFF,GameState.ENEMIES[i].bmdCopy,testPoint,0xFF))
				{
					state = NinjaState.NINJA_FALLING;
					Global.dispatchEvent(Msg_GameOver.M_GAMEOVER,Msg_GameOver.createMessage());
					
				}
					
				
			}
		}
			
	}
}